<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增温度采集器')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-temp-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">温度采集器ID：</label>
                <div class="col-sm-8">
                    <input name="tempId" class="form-control" type="number" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">安装地址：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="locationId" type="hidden" id="treeId" th:value="${location.locationId}"/>
                        <input name="locationName" onclick="selectLocationTree()" readonly="true" id="treeName" type="text" th:value="${location.locationName}" placeholder="请选择所属住址" class="form-control" required>
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">安装时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="installTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">设备厂商：</label>
                <div class="col-sm-8">
                    <select id="factoryId" name="factoryId" class="form-control">
                        <option th:each="factory:${factorys}" th:value="${factory.factoryId}" th:text="${factory.factoryName}"></option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "base/temp"
        $("#form-temp-add").validate({
            onkeyup: false,
            rules:{
                tempId:{
                    remote: {
                        url: prefix + "/checkTempUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "tempId": function() {
                                return $("input[name='tempId']").val();
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                }
            },
            messages: {
                "tempId": {
                    remote: "温度采集器已存在"
                }
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-temp-add').serialize());
            }
        }

        $("input[name='installTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        /*选择住址树*/
        function selectLocationTree() {
            var options = {
                title: '住址选择',
                width: "380",
                url: ctx + "base/location/selectLocationTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);

            queryTemp(body.find('#treeId').val());
        }

        /**
         * 根据地址查询热表
         */
        function queryTemp(locationId){
            $.ajax({
                url: prefix + "/selectTempByLocation/"+locationId,
                cache: false,
                contentType: false,
                processData: false,
                type: 'POST',
                success: function (result) {
                    if(result != ''){
                        $.modal.alertError("此地址下包含热表设备："+ result + "，请重新选择！");
                        $("#treeId").val("");
                        $("#treeName").val("");
                    }
                }
            });
        }
    </script>
</body>
</html>